package com.itheima.controller;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
  /*  @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10") Integer pageSize
    ){
        log.info("查询员工信息, page={}, pageSize={}", page, pageSize);
        PageBean pageBean = empService.page(page, pageSize);
        return Result.success(pageBean);
    }*/
    @GetMapping
    public Result page(EmpQueryParam param){
        log.info("查询员工请求参数: {}", param);
        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("添加员工请求参数: {}", emp);
        empService.add(emp);
        return Result.success();
    }
    @DeleteMapping
    public Result deleteById(@RequestParam List<Integer> ids )  {
        log.info("根据ID批量删除员工:ids={} " , ids);
        empService.deleteByIds(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询员工: {}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工请求参数: {}", emp);
        empService.update(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result pList (){
        log.info("查询所有员工");
        List<Emp> empList = empService.pList();
        return Result.success(empList);
    }
}

